package com.yida.gwj.scene.expend.controller;

import java.io.IOException;
import java.util.List;

import javax.servlet.http.HttpServletResponse;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.DeleteMapping;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.PutMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.multipart.MultipartFile;

import com.yida.common.core.utils.StringUtils;
import com.yida.common.core.utils.file.FileTypeUtils;
import com.yida.common.core.utils.poi.ExcelUtil;
import com.yida.common.core.web.controller.BaseController;
import com.yida.common.core.web.domain.AjaxResult;
import com.yida.common.log.annotation.Log;
import com.yida.common.log.enums.BusinessType;
import com.yida.common.security.annotation.RequiresPermissions;
import com.yida.gwj.scene.expend.domain.GwjSceneExpendBusinessHouseAndOwner;
import com.yida.gwj.scene.expend.domain.request.GwjSceneExpendBusinessRequest;
import com.yida.gwj.scene.expend.domain.request.GwjSceneExpendHouseAndOwnerRequest;
import com.yida.gwj.scene.expend.domain.request.GwjSceneExpendShareEquallyMoneyRequest;
import com.yida.gwj.scene.expend.domain.request.GwjSceneExpendSubmitRequest;
import com.yida.gwj.scene.expend.service.GwjSceneExpendBusinessMainService;

import cn.hutool.core.io.IORuntimeException;

/**
 * 
 * 线下业务主处理接口
 * 
 * @author hmh
 * @date 2023/08/14
 */
@RestController
@RequestMapping("/main")
public class GwjSceneExpendBusinessMainController extends BaseController {

    @Autowired
    private GwjSceneExpendBusinessMainService gwjSceneExpendBusinessMainService;

    /**
     * 业务保存
     * 
     * @param gwjSceneExpendBusinessRequest 业务信息请求
     * @return
     */
    @Log(title = "线下支出", businessType = BusinessType.INSERT, systemName = "gwj")
    @RequiresPermissions("gwj:scene:expend:owner:add")
    @PostMapping("/save")
    public AjaxResult saveBusiness(@RequestBody GwjSceneExpendBusinessRequest gwjSceneExpendBusinessRequest) {
        gwjSceneExpendBusinessMainService.saveBusiness(gwjSceneExpendBusinessRequest);
        return AjaxResult.success();
    }

    /**
     * 查询详情
     * 
     * @param bizId 业务ID
     * @param areaId 地区ID
     * @return
     */
    @GetMapping("/info")
    public AjaxResult selectBusiness(@RequestParam("bizId") Long bizId, @RequestParam("areaId") Long areaId) {
        return AjaxResult.success(gwjSceneExpendBusinessMainService.selectBusiness(bizId, areaId));
    }

    /**
     * 修改业务信息
     * 
     * @param gwjSceneExpendBusinessRequest 业务信息请求
     * @return
     */
    @Log(title = "线下支出", businessType = BusinessType.UPDATE, systemName = "gwj")
    @RequiresPermissions("gwj:scene:expend:owner:edit")
    @PutMapping("/edit")
    public AjaxResult editBusiness(@RequestBody GwjSceneExpendBusinessRequest gwjSceneExpendBusinessRequest) {
        gwjSceneExpendBusinessMainService.editBusiness(gwjSceneExpendBusinessRequest);
        return AjaxResult.success();
    }

    /**
     * 坑爹提交
     * 
     * @param gwjSceneExpendSubmitRequest 提交请求类
     * @return
     */
    @Log(title = "线下支出", businessType = BusinessType.UPDATE, systemName = "gwj")
    @RequiresPermissions("gwj:scene:expend:owner:edit")
    @PostMapping("/submit")
    public AjaxResult submitBusiness(@RequestBody GwjSceneExpendSubmitRequest gwjSceneExpendSubmitRequest) {
        gwjSceneExpendBusinessMainService.submitBusiness(gwjSceneExpendSubmitRequest);
        return AjaxResult.success();
    }

    /**
     * 回退
     * 
     * @param gwjSceneExpendSubmitRequest 提交请求类
     * @return
     */
    @Log(title = "线下支出", businessType = BusinessType.UPDATE, systemName = "gwj")
    @RequiresPermissions("gwj:scene:expend:owner:edit")
    @PutMapping("/return")
    public AjaxResult returnBusiness(@RequestBody GwjSceneExpendSubmitRequest gwjSceneExpendSubmitRequest) {
        gwjSceneExpendBusinessMainService.returnBusiness(gwjSceneExpendSubmitRequest);
        return AjaxResult.success();
    }

    /**
     * 送件操作
     * 
     * @param gwjSceneExpendSubmitRequest 提交请求类
     * @return
     */
    @Log(title = "线下支出", businessType = BusinessType.UPDATE, systemName = "gwj")
    @RequiresPermissions("gwj:scene:expend:owner:edit")
    @PutMapping("/change")
    public AjaxResult changeOper(@RequestBody GwjSceneExpendSubmitRequest gwjSceneExpendSubmitRequest) {
        gwjSceneExpendBusinessMainService.changeOper(gwjSceneExpendSubmitRequest);
        return AjaxResult.success();
    }

    /**
     * 删除案例
     * 
     * @param gwjSceneExpendSubmitRequest
     * @return
     */
    @Log(title = "线下支出", businessType = BusinessType.DELETE, systemName = "gwj")
    @RequiresPermissions("gwj:scene:expend:owner:remove")
    @DeleteMapping
    public AjaxResult deleteBusiness(@RequestBody GwjSceneExpendSubmitRequest gwjSceneExpendSubmitRequest) {
        gwjSceneExpendBusinessMainService.deleteBusiness(gwjSceneExpendSubmitRequest);
        return AjaxResult.success();
    }

    /**
     * 按类型分摊金额
     * 
     * @param gwjSceneExpendShareEquallyMoneyRequest 分摊金额请求
     * @return
     */
    @Log(title = "线下支出", businessType = BusinessType.UPDATE, systemName = "gwj")
    @RequiresPermissions("gwj:scene:expend:owner:edit")
    @PutMapping("/shareEquallyMoney")
    public AjaxResult shareEquallyMoney(@RequestBody GwjSceneExpendShareEquallyMoneyRequest gwjSceneExpendShareEquallyMoneyRequest) {
        gwjSceneExpendBusinessMainService.shareEquallyMoney(gwjSceneExpendShareEquallyMoneyRequest);
        return AjaxResult.success();
    }

    /**
     * 财务对账
     * 
     * @param gwjSceneExpendSubmitRequest 提交请求类
     * @return
     */
    @Log(title = "线下支出", businessType = BusinessType.UPDATE, systemName = "gwj")
    @RequiresPermissions("gwj:scene:expend:owner:edit")
    @PostMapping("/financeCheck")
    public AjaxResult financeCheck(@RequestBody GwjSceneExpendSubmitRequest gwjSceneExpendSubmitRequest) {
        gwjSceneExpendBusinessMainService.financeCheck(gwjSceneExpendSubmitRequest.getBizId(), gwjSceneExpendSubmitRequest.getAreaId());
        return AjaxResult.success();
    }

    /**
     * 获取业务步骤配置信息
     * 
     * @return
     */
    @GetMapping("/stepInfo")
    public AjaxResult stepInfo() {
        return AjaxResult.success(gwjSceneExpendBusinessMainService.stepInfo());
    }

    // 查询总账内网改用yida-gwj-finance模块下的GwjFinanceAccountMainController list方法调用

    // 查询明细账内网改用yida-gwj-finance模块下的GwjFinanceAccountDetailController detailList方法调用

    // 导入20230906
    @Log(title = "线下支出", businessType = BusinessType.IMPORT, systemName = "gwj")
    @RequiresPermissions("gwj:scene:expend:import")
    @PostMapping("/importData")
    public AjaxResult importData(MultipartFile file, @RequestParam(name = "updateSupport") boolean updateSupport, @RequestParam(name = "bizId") Long bizId) throws Exception {
        if (file.isEmpty()) {
            return AjaxResult.error("上传文件不能为空！");
        }
        if (!checkImportFile(file)) {
            return AjaxResult.error("不是指定的xlsx，xls导入类型文件！");
        }
        String result = gwjSceneExpendBusinessMainService.importHouseList(file, updateSupport, bizId);
        return AjaxResult.success(result);
    }

    // 导出20230906
    // @Log(title = "线下支出", businessType = BusinessType.EXPORT, systemName = "gwj")
    // @RequiresPermissions("gwj:scene:expend:export")
    // @PostMapping("/export")
    // public void export(HttpServletResponse response, @RequestBody GwjSceneExpendBusinessHouseAndOwnerImp
    // gwjSceneExpendBusinessHouseAndOwnerImp) {
    // List<GwjSceneExpendBusinessHouseAndOwnerImp> list =
    // gwjMaintenanceFundsBusinessService.exportHouseList(allocatedAmountRequest);
    // ExcelUtil<AllocatedAmountListDTO> util = new ExcelUtil<>(AllocatedAmountListDTO.class);
    // util.exportExcel(response, list, "公维金维修资金业务房屋数据");
    // }

    /**
     * 导出房屋列表
     */
    @RequiresPermissions("gwj:scene:expend:export")
    @Log(title = "线下支出", businessType = BusinessType.EXPORT, systemName = "gwj")
    @PostMapping("/export")
    public void export(HttpServletResponse response, GwjSceneExpendHouseAndOwnerRequest gwjSceneExpendHouseAndOwnerRequest) {
        List<GwjSceneExpendBusinessHouseAndOwner> list = gwjSceneExpendBusinessMainService.exportHouseList(gwjSceneExpendHouseAndOwnerRequest);
        ExcelUtil<GwjSceneExpendBusinessHouseAndOwner> util = new ExcelUtil<GwjSceneExpendBusinessHouseAndOwner>(GwjSceneExpendBusinessHouseAndOwner.class);
        util.exportExcel(response, list, "业务数据");
    }

    /**
     * 检测导入文件类型是否合规
     * 
     * @param file
     * @return
     * @throws IOException
     * @throws IORuntimeException
     */
    private boolean checkImportFile(MultipartFile file) throws Exception {
        // 检查文件类型
        // String type = FileTypeUtil.getType(file.getInputStream());
        String type = FileTypeUtils.getExtension(file);
        // 限制的文件类型集合
        if (StringUtils.inStringIgnoreCase(type, "xlsx", "xls")) {
            return true;
        }
        return false;
    }

}
